Ext.ns("RatingProjectSetView");
var RatingProjectSetView = function() {
    return this.setup();
};

RatingProjectSetView.prototype.setup = function() {
    var f;
    var l = new Ext.data.Store({
        proxy : new Ext.data.HttpProxy({
            url : __ctxPath + "/safeachievements/listRatingProjectSet.do"
        }),
        reader : new Ext.data.JsonReader({
            root : "result",
            totalProperty : "totalCounts",
            fields : [ {
                name : "id",
                type : "int"
            },  "oneLevel", "twoLevel","regulation","baseStandard","standardScore","scoreModel","levels" ]
        }),
        remoteSort : true
    });
    l.setDefaultSort("id", "desc");
    l.load({
        params : {
            start : 0,
            limit : 25
        }
    });
    
    var e = new Ext.grid.CheckboxSelectionModel();
    var m = new Ext.grid.ColumnModel(
            {
                columns : [
                           e,
                           {
                               header : "id",
                               dataIndex : "id",
                               hidden : true,
                               renderer : function(o) {
                                   return o.id != null ? o.id : "";
                               }
                           },
                           {
                               header : "ratingProjectSetTreeId",
                               dataIndex : "ratingProjectSetTreeId",
                               hidden : true
                           },
                           {
                               header : "一级要素",
                               sortable : true,
                               dataIndex : "oneLevel",
                               width : 60
                           },
                           {
                               header : "二级要素",
                               dataIndex : "twoLevel",
                               width : 60
                           },
                           {
                               header : "基本规范要求",
                               sortable : true,
                               dataIndex : "regulation",
                               width : 60
                           },
                           {
                               header : "企业达标标准",
                               dataIndex : "baseStandard",
                               width : 60
                           },
                           {
                               header : "标准分值",
                               dataIndex : "standardScore",
                               width : 60
                           },
                           {
                               header : "管理",
                               dataIndex : "appUser",
                               sortable : true,
                               width : 100,
                               renderer : function(x, t, p, s, v, u) {
                                   var r = "";
                                   if(isGranted("_ratingProjectSetDel")){
                                   r += '<button title="删除" value=" "  class="btn-delete" onclick="RatingProjectSetView.remove('
                                            + p.id
                                            + ')"></button>';
                                   }
                                   if(isGranted("_ratingProjectSetEdit")){
                                    r += '&nbsp;<button title="编辑" value=" "  class="btn-edit" onclick="RatingProjectSetView.edit('+ p.id+ ','+p.json.ratingProjectSetTreeId+')"></button>';
                                   }
                                    return r;
                                   
                               }
                           }],
                defaults : {
                    sortable : true,
                    menuDisabled : true,
                    width : 100
                },
                listeners : {
                    hiddenchange : function(o, p, q) {
                        saveConfig(p, q);
                    }
                }
            });
    var c = new Ext.grid.GridPanel({
        region : "center",
        id : "RatingProjectSetView",
        tbar : new Ext.Toolbar({
            defaultType : "button",
            items : [ {
                text : "添加",
                iconCls : "add-user",
                handler : function() {
                    var f = Ext.getCmp("ratingProjectSetTreePanel");
                    var g = f.getSelectionModel().getSelectedNode();
                    var h = 0;
                    if (g != null) {
                        h = g.id;
                    }
                    if (h == 0) {
                        Ext.ux.Toast.msg(
                                "操作信息",
                                "请从左选择项目名称！");
                        return;
                    }
                    new RatingProjectSetGridForm({
                        ratingProjectSetTreeId : h,
                        name : g.text
                    }).show();
                }
            }, "-", {
                text : "删除",
                iconCls : "btn-delete",
                handler : function() {
                    RatingProjectSetView.multiRemove();
                }
            } ]
        }),
        height : 800,
        title : "自评项目设置列表",
        store : l,
        shim : true,
        trackMouseOver : true,
        disableSelection : false,
        loadMask : true,
        cm : m,
        sm : e,
        viewConfig : {
            forceFit : true,
            enableRowBody : false,
            showPreview : false
        },
        bbar : new HT.PagingBar({
            store : l
        })
    });
    function h(p, o, q) {
        p.getSelectionModel().each(
                function(r) {
                    RatingProjectSetView.edit(r.data.appUser.userId,
                            r.data.appUser.username);
                });
    }

    var g = new Ext.tree.TreePanel({
        region : "west",
        id : "ratingProjectSetTreePanel",
        title : "自评项目名称列表",
        collapsible : true,
        autoScroll : true,
        split : true,
        height : 800,
        width : 180,
        tbar : new Ext.Toolbar({
            items : [ {
                xtype : "button",
                iconCls : "btn-refresh",
                text : "刷新",
                handler : function() {
                    g.root.reload();
                }
            }, "-", {
                xtype : "button",
                text : "展开",
                iconCls : "btn-expand",
                handler : function() {
                    g.expandAll();
                }
            }, "-", {
                xtype : "button",
                text : "收起",
                iconCls : "btn-collapse",
                handler : function() {
                    g.collapseAll();
                }
            } ]
        }),
        loader : new Ext.tree.TreeLoader({
            url : __ctxPath + "/safeachievements/getAllRatingProjectSetTree.do"
        }),
        root : new Ext.tree.AsyncTreeNode({
            expanded : true
        }),
        rootVisible : false,
        listeners : {
            "click" : RatingProjectSetView.clickNode
        }
    });
    if (isGranted("_ratingProjectSetAdd") || isGranted("_ratingProjectSetEdit")
            || isGranted("_ratingProjectSetDel")) {
        g.on("contextmenu", b, g);
    }
    function b(o, p) {
        f = new Ext.tree.TreeNode({
            id : o.id,
            text : o.text
        });
        var q = new Ext.menu.Menu({
            items : []
        });
        q.clearMons();
        if (isGranted("_ratingProjectSetAdd")) {
            q.add({
                text : "新建项目名称信息",
                iconCls : "btn-add",
                scope : this,
                handler : n
            });
        }
        if (isGranted("_ratingProjectSetEdit")) {
            q.add({
                text : "修改项目名称信息",
                iconCls : "btn-edit",
                scope : this,
                handler : j
            });
        }
        if (isGranted("_ratingProjectSetDel")) {
            q.add({
                text : "删除项目名称",
                iconCls : "btn-delete",
                scope : this,
                handler : k
            });
        }
        q.showAt(p.getXY());
    }
    function n() {
        var o = f.id;
        var p = Ext.getCmp("ratingProjectSetForm");
        if (p == null) {
            if (o > 1) {
                new RatingProjectSetForm({
                    nodeId : o
                }).show();
            } else {
                new RatingProjectSetForm({
                    nodeId : 1
                }).show();
            }
            Ext.getCmp("ratingProjectSetTreePanel").root.reload();
        }
    }
    function k() {
        var o = f.id;
        if (o > 1) {
            Ext.Msg.confirm("删除操作", "你确定删除部门?", function(p) {
                if (p == "yes") {
                    Ext.Ajax.request({
                        url : __ctxPath + "/safeachievements/deleteRatingProjectSetTree.do?id="
                                + o,
                        success : function(q, s) {
                            var r = Ext.util.JSON.decode(q.responseText);
                            if (r.success == false) {
                                Ext.MessageBox.show({
                                    title : "操作提示",
                                    msg : r.msg,
                                    buttons : Ext.MessageBox.OK,
                                    icon : "ext-mb-error"
                                });                             
                            } else {
                                Ext.ux.Toast.msg("操作提示", "删除成功!");
                            }
                            Ext.getCmp("ratingProjectSetTreePanel").root.reload();
                        },
                        failure : function(q, r) {
                            Ext.ux.Toast.msg("操作提示", "删除失败!");
                        }
                    });
                }
            });
        } 
    }
    function j() {
        var o = f.id;
        if (o > 1) {
            var p = Ext.getCmp("ratingProjectSetForm");
            if (p == null) {
                new RatingProjectSetForm().show();
                p = Ext.getCmp("ratingProjectSetForm");
            }
                p.loadData({
                url : __ctxPath + "/safeachievements/getRatingProjectSetTree.do?ratingProjectSetTree.id="+o,
                preName : "ratingProjectSetTree",
                root : "result",
                success : function(form, action) {
                    
                } 
            
            });
        } 
    }
    var d = new Ext.FormPanel({
        id : "ratingProjectSetViewSearchPanel",
        height : 40,
        frame : false,
        border : false,
        region : "north",
        layout : "form",
        layoutConfig : {
            padding : "5px",
            align : "middle"
        },
        items : [ {
            xtype : "container",
            layout : "column",
            border : false,
            fieldLabel : "请输入查询条件",
            style : "margin-top:8px;",
            defaults : {
                xtype : "label",
                border : false,
                height : 25
            },
            layoutConfig : {
                align : "middle"
            },
            items : [ {
                style : "margin:5px 5px 5px 5px",
                text : "二级要素"
            }, {
                columnWidth : 0.2,
                xtype : "textfield",
                name : "Q_twoLevel_S_LK",
                maxLength : 256
            }, {
                style : "margin-left: 5px;",
                xtype : "button",
                text : "搜索",
                scope : this,
                iconCls : "search",
                handler : RatingProjectSetView.search
            }, {
                xtype : "button",
                text : "清空",
                scope : this,
                iconCls : "reset",
                handler : RatingProjectSetView.reset
            } ]
        } ]
    });
    
    
    //载入前带查询条件的翻页

    Ext.getCmp("RatingProjectSetView").getStore().on('beforeload', function(store, options) {  
        var p = Ext.getCmp("ratingProjectSetTreePanel").getSelectionModel().getSelectedNode();
        var e = Ext.getCmp("ratingProjectSetViewSearchPanel");
        var b = e.getCmpByName("Q_twoLevel_S_LK").getValue();
        var new_params = {
                "Q_twoLevel_S_LK" : b
        }; 

    if (p != null && p.id > 1) {
        new_params["Q_ratingProjectSetTreeId_L_EQ"] = p.id;
    }

    Ext.apply(options.params, new_params);  
    
    });

    
    
    var a = new Ext.Panel({
        id : "RatingProjectSetTView",
        title : "自评项目设置管理",
        closable : true,
        iconCls : "menu-department",
        layout : "border",
        items : [ g, d, c ],
        keys : [ {
            key : Ext.EventObject.ESC,
            fn : RatingProjectSetView.reset,
            scope : this
        }, {
            key : Ext.EventObject.ENTER,
            fn : RatingProjectSetView.search,
            scope : this
        } ]
    });
    return a;
};

RatingProjectSetView.multiRemove = function() {
    var b = Ext.getCmp("RatingProjectSetView");
    var d = b.getSelectionModel().getSelections();
    if (d != null && d.length > 0) {
        var c = new Array();
        for ( var a = 0; a < d.length; a++) {
            c.push(d[a].data.id);
        }
        RatingProjectSetView.remove(c);
    } else {
        Ext.ux.Toast.msg("操作提示", "对不起，请选择你要删除的数据！");
    }
}; 
RatingProjectSetView.remove = function(a) {
    Ext.Msg.confirm("删除操作", "你确定要删除该自评项目详细信息吗?", function(b) {
        if (b == "yes") {
            Ext.Ajax.request({
                url : __ctxPath + "/safeachievements/deleteRatingProjectSet.do",
                method : "post",
                params : {
                    ids : a
                },
                success : function(c) {
                    Ext.ux.Toast.msg("操作提示", "自评项目详细信息删除成功");
                    Ext.getCmp("RatingProjectSetView").getStore().reload();
                },
                failure : function() {
                    Ext.ux.Toast.msg("操作提示", "用户删除失败");
                }
            });
        }
    });
};
RatingProjectSetView.clickNode = function(a) {
    RatingProjectSetView.select(a);
};
RatingProjectSetView.reset = function() {
    var a = Ext.getCmp("ratingProjectSetViewSearchPanel");
    a.getForm().reset();
};
RatingProjectSetView.search = function() {
    RatingProjectSetView.select(null);
};
RatingProjectSetView.select = function(f) {
    
    if(f){
        Ext.getCmp("ratingProjectSetTreePanel").getSelectionModel().clearSelections();    
        Ext.getCmp("ratingProjectSetTreePanel").getSelectionModel().select(f);}
    var g = Ext.getCmp("RatingProjectSetView");
    var c = g.getStore();
    
    var d = {
        start : 0,
        limit : 25
    };
    c.reload({
        params : d
    });
};
RatingProjectSetView.edit = function(a,ratingProjectSetTreeId) {
   new RatingProjectSetGridForm({
        id : a,
        ratingProjectSetTreeId:ratingProjectSetTreeId
    }).show();
};
RatingProjectSetView.show = function(a, b) {
    RatingProjectSetGridForm.show(a, b);
};


RatingProjectSetView.onRowActions = function(c, a, d, e, b) {
    switch (d) {
    case "btn-edit":
        RatingProjectSetView.edit.call(this, a);
        break;
    /*case "btn-delete":
        RatingProjectSetView.removeById.call(this, a.data.id);
        break;*/
    default:
        break;
    }
};

RatingProjectSetView.editById = function(a) {
    var inputName = option.topbar.inputFormName;
    var cfg = {
        id : a.data.id,
        operation : Constant.Operation.EDIT,
        inputFormName : option.topbar.inputFormName,
        addYearGridPanel : this.yearGridPanel.getId()
    };
    var str = "new " + inputName + "(" + JSON.stringify(cfg) + ")";
    eval(str).show();
};

RatingProjectSetView.removeById = function(a) {
    Ext.Msg.confirm("操作提示", "你真的要删除所选数据吗？", function(b) {
        if (b == "yes") {
            Ext.Ajax.request({
                url : option.grid.removeUrl,
                params : {
                    ids : a
                },
                scope : this,
                method : "post",
                waitMsg : "数据正在提交，请稍后...",
                success : function(c, d) {
                    Ext.ux.Toast.msg("操作提示", "数据删除成功！");
                    this.yearGridPanel.getStore().reload();
                },
                failure : function() {
                    Ext.ux.Toast.msg("操作提示", "对不起，数据提交失败！");
                }
            });
        }
    }, this);
};
